package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.MyBean;
import com.realbrand.net.realbrandpay.bean.ScanBean;
import com.realbrand.net.realbrandpay.bean.ShopcarBean;
import com.realbrand.net.realbrandpay.db.DBManger;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.CommodityDetailsActivity;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.zxing.util.L;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/6/23.
 */

public class ShopCarAdapter extends BaseExpandableListAdapter {
    List<List<ScanBean>> datas;
    List<MyBean> list;
    Context context;
    LayoutInflater inflater;
    DBManger dbManger;
    //设置接口
    private View.OnClickListener onAddNum;//加商品数量的接口
    private View.OnClickListener onSubNum;//减商品数量的接口
    private View.OnClickListener onRemove;//删除整个item的接口

    //创建接口方法
    public void setOnAddNum(View.OnClickListener onAddNum) {
        this.onAddNum = onAddNum;
    }

    public void setOnSubNum(View.OnClickListener onSubNum) {
        this.onSubNum = onSubNum;
    }

    public void setOnRemove(View.OnClickListener onRemove) {
        this.onRemove = onRemove;
    }

    public ShopCarAdapter(List<List<ScanBean>> datas, List<MyBean> list, Context context, DBManger dbManger) {
        this.datas = datas;
        this.list = list;
        this.context = context;
        this.dbManger = dbManger;
        inflater = LayoutInflater.from(context);
    }

    //返回一级列表的个数
    @Override
    public int getGroupCount() {
        return list.size();
    }

    //返回每个二级列表的个数
    @Override
    public int getChildrenCount(int groupPosition) {//参数表示第几个一级列表
        return 1;
    }

    //返回一级列表的单个item
    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    //返回二级列表中单个的item
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //每个item的id是否固定？一般为true
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //填充一级列表
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHelder groupViewHelder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.shopcarlistview_item, parent, false);
            groupViewHelder = new GroupViewHelder(convertView);
            groupViewHelder.item_btn_sub.setOnClickListener(onSubNum);
            groupViewHelder.item_btn_add.setOnClickListener(onAddNum);
            groupViewHelder.item_img_remove.setOnClickListener(onRemove);
            convertView.setTag(groupViewHelder);
        } else {
            groupViewHelder = (GroupViewHelder) convertView.getTag();
        }
        groupViewHelder.item_shopcarBean_storeName.setText(list.get(groupPosition).getBean().getStore());
        groupViewHelder.item_shopcarBean_storeAdress.setText(list.get(groupPosition).getBean().getAdress());
        groupViewHelder.item_shopcarBean_price.setText(list.get(groupPosition).getBean().getPrice());
        groupViewHelder.item_shopcarBean_number.setText(list.get(groupPosition).getCount() + "");
        groupViewHelder.item_shopcarBean_commdityName.setText(list.get(groupPosition).getBean().getName());
//        groupViewHelder.item_shopcarBean_iv.setImageResource(list.get(groupPosition).getBean().getDrawableId());

        String[] strings = list.get(groupPosition).getBean().getDrawableId().split(",");
        Picasso.with(context).load(strings[0] + strings[1]).
                into(groupViewHelder.item_shopcarBean_iv);
        //设置tag，用于判断用户当前点击的哪一个列表项的按钮
        groupViewHelder.item_btn_sub.setTag(groupPosition);
        groupViewHelder.item_btn_add.setTag(groupPosition);
        groupViewHelder.item_img_remove.setTag(groupPosition);
        return convertView;
    }

    //填充二级列表
    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHelder childViewHelder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.shopcar_elv_item_layout, parent, false);
            childViewHelder = new ChildViewHelder(convertView);
            convertView.setTag(childViewHelder);
        } else {
            childViewHelder = (ChildViewHelder) convertView.getTag();
        }
        //设置listView
        ShopcarListViewAdapter listViewAdapter = new ShopcarListViewAdapter(datas.get(groupPosition), inflater);
        L.d(L.TAG,"子项的个数"+datas.get(groupPosition).toString());
        childViewHelder.listView.setAdapter(listViewAdapter);
        setListViewHeightBasedOnChildren(childViewHelder.listView);
        childViewHelder.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String scan = datas.get(groupPosition).get(position).getScan();
                ShopcarBean bean = dbManger.selectScan(scan);
                Intent intent = new Intent(context, CommodityDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", bean);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    //二级列表中的item是否能被选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ChildViewHelder {
        private ListView listView;

        public ChildViewHelder(View view) {
            listView = (ListView) view.findViewById(R.id.shopcarElvItem_listView);
        }
    }

    class GroupViewHelder {
        //商品图片，数量，总价，商品名称，销售店名称，销售店地址
        private ImageView item_shopcarBean_iv;
        private TextView item_shopcarBean_commdityName;
        private TextView item_shopcarBean_storeName;
        private TextView item_shopcarBean_storeAdress;
        private TextView item_shopcarBean_price;
        private TextView item_shopcarBean_number;
        //增减商品数量的按钮
        private ImageButton item_btn_add;
        private ImageButton item_btn_sub;
        //删除跟个Item
        private ImageButton item_img_remove;


        public GroupViewHelder(View view) {
            item_shopcarBean_commdityName = (TextView) view.findViewById(R.id.shopCar_tv_listview_item_commodityName);
            item_shopcarBean_iv = (ImageView) view.findViewById(R.id.shopcar_iv_listview_item);
            item_shopcarBean_number = (TextView) view.findViewById(R.id.shopcar_tv_listview_item_number);
            item_shopcarBean_price = (TextView) view.findViewById(R.id.shopCar_tv_listview_item_money);
            item_shopcarBean_storeAdress = (TextView) view.findViewById(R.id.shopCar_tv_listview_item_storeAdress);
            item_shopcarBean_storeName = (TextView) view.findViewById(R.id.shopCar_tv_listview_item_storeName);
            item_btn_add = (ImageButton) view.findViewById(R.id.shopcar_bt_listview_item_add);
            item_btn_sub = (ImageButton) view.findViewById(R.id.shopcar_bt_listview_item_minus);
            item_img_remove = (ImageButton) view.findViewById(R.id.shopcar_bt_listview_item_remove);
        }
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
// 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
